package cn.vcjf.weapp.api.goods.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.goods.entity.GoodsDiscount;
import cn.vcjf.weapp.api.goods.service.GoodsDiscountService;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 优惠劵
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/goods/goodsDiscount")
public class GoodsDiscountController {
	@Resource
	private GoodsDiscountService goodsDiscountService;

	@RequestMapping("/findGoodsDiscountOne")
	@ResponseBody
	public TableResult findGoodsDiscountOne(GoodsDiscount goodsDiscount) {

		EntityWrapper<GoodsDiscount> wrapper = new EntityWrapper<GoodsDiscount>();

		if (goodsDiscount.getId() != null) {
			wrapper.eq("id", goodsDiscount.getId());
		}
		//Todo

		GoodsDiscount result = goodsDiscountService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	/**
	 * 领取优惠券
	 * @param goodsDiscount
	 * @return
	 */
	@RequestMapping("/getGoodsDiscount")
	@ResponseBody
	public TableResult getGoodsDiscount(GoodsDiscount goodsDiscount,Integer userId) {

		TableResult result =  goodsDiscountService.addGoodsDiscount(goodsDiscount,userId);

		return result;
	}

	/**
	 * 获取优惠券列表
	 *
	 * @param goodsDiscount
	 * @return
	 */
	@RequestMapping("/findGoodsDiscountList")
	@ResponseBody
	public TableResult findGoodsDiscountList(GoodsDiscount goodsDiscount) {

		EntityWrapper<GoodsDiscount> wrapper = new EntityWrapper<GoodsDiscount>();

		wrapper.eq("sysUserId", goodsDiscount.getSysUserId());
		if(goodsDiscount.getDiscountType()!=null) {
			if (goodsDiscount.getDiscountType() <= 2 ) {
				if (goodsDiscount.getDiscountType() == 1) {
					wrapper.eq("goodsId", goodsDiscount.getGoodsId());
				}
				wrapper.eq("goodsTypeId", goodsDiscount.getGoodsTypeId());
			}
			//优惠券类型(1,商品,2分类,3全场)
			wrapper.eq("discountType", goodsDiscount.getDiscountType());
		}
		Page<GoodsDiscount> page = goodsDiscount.getPageInfo();

		page = goodsDiscountService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

}
